package leetcode.每日一题;

import org.apache.commons.collections4.comparators.ReverseComparator;
import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/12/2 10:07
 * @description：https://leetcode-cn.com/problems/relative-ranks/
 */
public class 相对名次 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(Arrays.toString(solution.findRelativeRanks(new int[]{10, 3, 8, 9, 4})));
    }

    /*
    class Solution {
        public String[] findRelativeRanks(int[] score) {
            int len = score.length;
            int[] scoreSort = score.clone();
            Arrays.sort(scoreSort);

            String[] ans = new String[len];
            for (int i = 0; i < len; i++) {
                int index = len - binarySearch(scoreSort, score[i]);
                System.out.println(index);
                switch (index) {
                    case 1:
                        ans[i] = "Gold Medal";
                        break;
                    case 2:
                        ans[i] = "Silver Medal";
                        break;
                    case 3:
                        ans[i] = "Bronze Medal";
                        break;
                    default:
                        ans[i] = index + "";
                }
            }
            return ans;
        }

        public int binarySearch(int[] nums, int tar) {
            int l = 0;
            int r = nums.length - 1;
            while (l < r) {
                int mid = l + r + 1 >> 1;
                if (nums[mid] <= tar) {
                    l = mid;
                } else {
                    r = mid - 1;
                }
            }
            return nums[l] <= tar ? l : -1;
        }
    }
     */

    class Solution {
        public String[] findRelativeRanks(int[] score) {
            String[] decs = {"Gold Medal", "Silver Medal", "Bronze Medal"};
            int n = score.length;
            int[][] arr = new int[n][2];
            for (int i = 0; i < n; i++) {
                arr[i][0] = score[i];
                arr[i][1] = i;
            }
            Arrays.sort(arr, (o1, o2) -> o2[0] - o1[0]);
            String[] ans = new String[n];
            for (int i = 0; i < n; i++) {
                if (i >= 3) {
                    ans[arr[i][1]] = Integer.toString(i + 1);
                } else {
                    ans[arr[i][1]] = decs[i];
                }
            }
            return ans;
        }
    }


}
